package com.essc.serviceImpl;

import com.essc.dto.BuyerDto;
import com.essc.dto.OrderDto;
import com.essc.dto.SellerDto;
import com.essc.mapper.GoodsMapper;
import com.essc.mapper.OrderMapper;
import com.essc.mapper.UserMapper;
import com.essc.pojo.Goods;
import com.essc.pojo.Order;
import com.essc.pojo.User;
import com.essc.service.OrderService;
import com.essc.utils.OrderResult;
import com.essc.utils.PageResult;
import com.essc.utils.JsonResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.springframework.util.ObjectUtils.isEmpty;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired(required = false)
    private OrderMapper orderMapper;
    @Autowired(required = false)
    private GoodsMapper goodsMapper;
    @Autowired(required = false)
    private UserMapper userMapper;

    /*生成订单（处理中 status=1）*/
    /*去购买 检查订单信息*/
    /*新增：自己发布的商品自己是买不了的*/
    @Override
    public JsonResult makeOrder(Order order){
        //判断获取的商品id与用户id是否为空
        if (isEmpty(order.getGoodId())||isEmpty(order.getUserId())){
            return new JsonResult(201,"订单生成失败");
        }
        //获取商品信息
        Goods goodsInfo = goodsMapper.findGoodById(order.getGoodId());
        if (goodsInfo ==null){
            return new JsonResult(203,"商品不存在");
        }
        //获取买家用户信息
        /*User buyerInfo = userMapper.findUserById(order.getUserId());*/
        BuyerDto buyerInfo = userMapper.findBuyerInfo(order.getUserId());
        if (buyerInfo==null){
            return new JsonResult(204,"会话超时，请重新登录");
        }
        //获取卖家用户信息
        User seller = userMapper.findUserById(goodsInfo.getUid());
        SellerDto sellerInfo = userMapper.findSellerInfo(goodsInfo.getUid());
        List<Order> orderList = orderMapper.findOrderInProgress(order.getUserId());
        //判断下单用户是否该商品的发布者
        if(seller.getUid()==order.getUserId()){
            return new JsonResult(207,"不能购买自己发布的商品！！");
        }
        //判断是否重复下单
        for(int i = 0; i < orderList.size(); i++){
            //遍历该用户所有进行中订单的商品id
            Order orderli = orderList.get(i);
            Integer goodId = orderli.getGoodId();
            //对比想要购买的商品id与该用户所有进行中订单的商品id
            if(order.getGoodId().equals(goodId)){
                return new JsonResult(205,"已存在该商品订单，请去支付");
            }
        }
        //生成处理订单 status=1
        int orderrow = orderMapper.makeOrder(order);
        if (orderrow!=1){
            return new JsonResult(202,"订单生成失败");
        }
        //获取订单id 传给前端用来做订单取消与支付
        Order orderInfo = orderMapper.findOwnerOrderInP(order.getUserId(), order.getGoodId());
        if(orderInfo==null){
            return new JsonResult(206,"找不到订单信息");
        }
        Integer oid = orderInfo.getOid();
        OrderDto orderDto = new OrderDto(oid, goodsInfo, sellerInfo, buyerInfo);
        return new JsonResult(200,"订单生成成功",orderDto);
    }

    //待支付显示订单详情 新增洪志成
    @Override
    public JsonResult showOrder(Order order){
        //获取商品信息
        Goods goodsInfo = goodsMapper.findGoodById(order.getGoodId());
        if (goodsInfo ==null){
            return new JsonResult(203,"商品不存在");
        }
        //获取买家用户信息
        /*User buyerInfo = userMapper.findUserById(order.getUserId());*/
        BuyerDto buyerInfo = userMapper.findBuyerInfo(order.getUserId());
        if (buyerInfo==null){
            return new JsonResult(204,"会话超时，请重新登录");
        }
        //获取卖家用户信息
        User seller = userMapper.findUserById(goodsInfo.getUid());
        SellerDto sellerInfo = userMapper.findSellerInfo(goodsInfo.getUid());
        List<Order> orderList = orderMapper.findOrderInProgress(order.getUserId());
        //获取订单id 传给前端用来做订单取消与支付
        Order orderInfo = orderMapper.findOwnerOrderInP(order.getUserId(), order.getGoodId());
        if(orderInfo==null){
            return new JsonResult(206,"找不到订单信息");
        }
        Integer oid = orderInfo.getOid();
        OrderDto orderDto = new OrderDto(oid, goodsInfo, sellerInfo, buyerInfo);
        return new JsonResult(200,"订单显示成功",orderDto);
    }

    /*去支付*/
    @Override
    public JsonResult payOrder(OrderDto orderDto) {
        System.out.println(orderDto);
        //判断前端传入的数据是否为空
        if (isEmpty(orderDto.getGoods().getGid())||isEmpty(orderDto.getBuyerDto().getBuyerId())||isEmpty(orderDto.getOid())
                ||isEmpty(orderDto.getSend())||isEmpty(orderDto.getTime())){
            return new JsonResult(201,"订单生成失败");
        }
        //获取商品信息
        Goods goodsInfo = goodsMapper.findGoodById(orderDto.getGoods().getGid());
        //获取买家用户信息
        User userInfo = userMapper.findUserById(orderDto.getBuyerDto().getBuyerId());
        //判断交易方式
        if(orderDto.getSend().equals("自提")){
            //修改订单交易方式为自提
            int row = orderMapper.orderBySelf(orderDto.getOid());
            if (row!=1){
                return new JsonResult(202,"选择自提失败");
            }
            //修改自提时间
            if (orderDto.getTime()==null){
                return new JsonResult(203,"请填写自提时间");
            }
            int row2 = orderMapper.updateApotime(orderDto.getOid(),orderDto.getTime());
            if (row2!=1){
                return new JsonResult(204,"请检查自提时间");
            }
        }else if (orderDto.getSend().equals("配送")){
            int row = orderMapper.orderBySend(orderDto.getOid());
            if (row!=1){
                return new JsonResult(205,"选择送货上门失败");
            }
            int row2 = orderMapper.updateSendTime(orderDto.getOid(), orderDto.getTime());
            if (row2!=1){
                return new JsonResult(206,"请填写上门时间");
            }
        }else {
            return new JsonResult(207,"交易方式仅支持自取或送货上门");
        }
        //支付
        if (goodsInfo.getPrice()<=userInfo.getMoney()){
            double balance;
            balance = userInfo.getMoney()- goodsInfo.getPrice();
            userInfo.setMoney(balance);
            //更新钱包  根据uid更新money
            int row = userMapper.deductMoney(userInfo.getUid(),userInfo.getMoney());
            //支付成功，订单完成（ status=2）
            int finishrow = orderMapper.finishOrder(orderDto.getOid());
            //支付成功下架商品
            goodsMapper.updateGood(goodsInfo.getGid());
            return new JsonResult(200,"支付成功！");
        }
        //余额不足支付失败，订单失效（ status=0）
        int cancelrow = orderMapper.cancelOrder(orderDto.getOid());
        return new JsonResult(202,"余额不足支付失败，请重试！");
    }


    /*取消订单（ status=0）*/
    @Override
    public JsonResult cancelOrder(Integer oid) {
        int cancelrow = orderMapper.cancelOrder(oid);
        return new JsonResult(200,"订单已被取消，已失效");
    }

    /*确认发货 （status=3）*/
    @Override
    public JsonResult confirmSendOrder(Integer oid) {
        Integer row = orderMapper.confirmSendOrder(oid);
        System.out.println(row);
        if(row!=1){
            return new JsonResult(201,"发货失败");
        }
        return new JsonResult(200,"发货成功，商品正在配送中");
    }

    /*确认签收 （status=4）*/
    @Override
    public JsonResult confirmReceipt(Integer oid) {
        //给卖家用户商品到款
        Order orderInfo = orderMapper.findOneOrder(oid);
        Goods goodInfo = goodsMapper.findGoodById(orderInfo.getGoodId());
        System.out.println(goodInfo.getUid());
        System.out.println(goodInfo.getPrice());
        int rechargerow = userMapper.recharge(goodInfo.getUid(), goodInfo.getPrice());
        if(rechargerow!=1){
            return new JsonResult(202,"商家用户到款失败");
        }
        //修改订单状态
        int row = orderMapper.confirmReceipt(oid);
        if(row!=1){
            return new JsonResult(201,"签收失败");
        }
        return new JsonResult(200,"商品签收成功");
    }

    /*发起退货请求 （status=-1）*/
    @Override
    public JsonResult requestGoodRejected(Integer oid) {
        int rejectrow = orderMapper.requestGoodRejected(oid);
        if(rejectrow!=1){
            return new JsonResult(201,"发起退款请求失败");
        }
        return new JsonResult(200,"发起退款请求成功");
    }

    /*同意退货 （status=0）*/
    @Override
    public JsonResult agreeGoodRejected(Integer oid) {
        //给卖家用户扣款
        Order orderInfo = orderMapper.findOneOrder(oid);
        Goods goodInfo = goodsMapper.findGoodById(orderInfo.getGoodId());
        //查询卖家用户余额
        User sellerInfo = userMapper.findUserById(goodInfo.getUid());
        if(sellerInfo.getMoney()<goodInfo.getPrice()){
            return new JsonResult(203,"卖家用户余额不足，退款失败。卖家快去充值！！再过来同意退款");
        }
        Double newMoney = sellerInfo.getMoney() - goodInfo.getPrice();
        //卖家用户扣款
        userMapper.deductMoney(goodInfo.getUid(),newMoney);
        //给买家用户退款
        User userInfo = userMapper.findUserById(orderInfo.getUserId());

        int rechargerow = userMapper.recharge(orderInfo.getUserId(), +goodInfo.getPrice());
        if(rechargerow!=1) {
            return new JsonResult(202, "买家用户到款失败");
        }
        int agreerow = orderMapper.agreeGoodRejected(oid);
        if(agreerow!=1){
            return new JsonResult(201,"卖家用户退款失败");
        }
        return new JsonResult(200,"退款成功");
    }

    /*---------------------------------买家用户查订单--------------------------------------*/

    /*查看待支付的订单（处理中 status=1）*/
    @Override
    public JsonResult findOrderInProgress(Integer uid, Integer pageNum, Integer pageSize) {
        //设置分页 pageNum当前页 pageSize每页数量
        PageHelper.startPage(pageNum,pageSize);
        List<Order> orderList = orderMapper.findOrderInProgress(uid);
        for (int i=0;i<orderList.size();i++){
            Order order2 = new Order();
            Order orderli = orderList.get(i);
            //获取用户信息
            User userInfo = userMapper.findUserById(uid);
            //买家名称
            String username = userInfo.getUsername();
            String tel = userInfo.getTel();
            String buyerLocation = userInfo.getLocation();
            order2.setTel(tel);
            order2.setBuyerLocation(buyerLocation);
            //获取商品信息
            Integer gid = orderli.getGoodId();
            Goods goodsInfo = goodsMapper.findGoodById(gid);
            //获取卖家信息
            Integer sellerId = goodsInfo.getUid();
            User sellerInfo = userMapper.findUserById(sellerId);
            String sellerName = sellerInfo.getUsername();
            String sellerLocation = sellerInfo.getLocation();
            String sellerTel = sellerInfo.getTel();
            order2.setSellerTel(sellerTel);
            order2.setSellerLocation(sellerLocation);
            order2.setSellerId(sellerId);
            order2.setSellerName(sellerName);
            String gname = goodsInfo.getGname();
            Double price = goodsInfo.getPrice();
            String pic = goodsInfo.getPic();
            //封装orderList
            order2.setOid(orderli.getOid());
            order2.setUserId(orderli.getUserId());
            order2.setUsername(username);
            order2.setGoodId(gid);
            order2.setGname(gname);
            order2.setPrice(price);
            order2.setPic(pic);
            order2.setCreateTime(orderli.getCreateTime());
            order2.setApoTime(orderli.getApoTime());
            order2.setSendTime(orderli.getSendTime());
            order2.setBySelf(orderli.getBySelf());
            order2.setBySend(orderli.getBySend());
            //订单状态
            order2.setStatus(orderli.getStatus());
            orderList.set(i,order2);
        }
        int totalSize = (int) ((Page)orderList).getTotal();
        PageResult pageResult = new PageResult(pageNum,pageSize,totalSize,orderList);
        if(orderList.size()==0){
            return new JsonResult(201,"您没有待处理订单");
        }
        return new JsonResult(200,"您有订单待处理",pageResult);
    }

    /*查看失效订单（ status=0）*/
    @Override
    public JsonResult findInvalidOrder(Integer uid, Integer pageNum, Integer pageSize) {
        //设置分页 pageNum当前页 pageSize每页数量
        PageHelper.startPage(pageNum,pageSize);
        List<Order> orderList = orderMapper.findInvalidOrder(uid);
        for (int i=0;i<orderList.size();i++){
            Order order2 = new Order();
            Order orderli = orderList.get(i);
            //获取用户信息
            User userInfo = userMapper.findUserById(uid);
            //买家名称
            String username = userInfo.getUsername();
            String tel = userInfo.getTel();
            String buyerLocation = userInfo.getLocation();
            order2.setTel(tel);
            order2.setBuyerLocation(buyerLocation);
            //获取商品信息
            Integer gid = orderli.getGoodId();
            Goods goodsInfo = goodsMapper.findGoodById(gid);
            //获取卖家信息
            Integer sellerId = goodsInfo.getUid();
            User sellerInfo = userMapper.findUserById(sellerId);
            String sellerName = sellerInfo.getUsername();
            String sellerLocation = sellerInfo.getLocation();
            String sellerTel = sellerInfo.getTel();
            order2.setSellerTel(sellerTel);
            order2.setSellerLocation(sellerLocation);
            order2.setSellerId(sellerId);
            order2.setSellerName(sellerName);
            String gname = goodsInfo.getGname();
            Double price = goodsInfo.getPrice();
            String pic = goodsInfo.getPic();
            //封装orderList
            order2.setOid(orderli.getOid());
            order2.setUserId(orderli.getUserId());
            order2.setUsername(username);
            order2.setGoodId(gid);
            order2.setGname(gname);
            order2.setPrice(price);
            order2.setPic(pic);
            order2.setCreateTime(orderli.getCreateTime());
            order2.setApoTime(orderli.getApoTime());
            order2.setSendTime(orderli.getSendTime());
            order2.setBySelf(orderli.getBySelf());
            order2.setBySend(orderli.getBySend());
            //订单状态
            order2.setStatus(orderli.getStatus());
            orderList.set(i,order2);
        }
        int totalSize = (int) ((Page)orderList).getTotal();
        PageResult pageResult = new PageResult(pageNum,pageSize,totalSize,orderList);
        if(orderList.size()==0){
            return new JsonResult(201,"您没有失效订单");
        }
        return new JsonResult(200,"您的失效订单",pageResult);
    }

    /*查看已支付订单（ status=2）*/
    @Override
    public JsonResult findCompletedOrder(Integer uid, Integer pageNum, Integer pageSize) {
        //设置分页 pageNum当前页 pageSize每页数量
        PageHelper.startPage(pageNum,pageSize);
        List<Order> orderList = orderMapper.findCompletedOrder(uid);
        for (int i=0;i<orderList.size();i++){
            Order order2 = new Order();
            Order orderli = orderList.get(i);
            //获取用户信息
            User userInfo = userMapper.findUserById(uid);
            //买家名称
            String username = userInfo.getUsername();
            String tel = userInfo.getTel();
            String buyerLocation = userInfo.getLocation();
            order2.setTel(tel);
            order2.setBuyerLocation(buyerLocation);
            //获取商品信息
            Integer gid = orderli.getGoodId();
            Goods goodsInfo = goodsMapper.findGoodById(gid);
            //获取卖家信息
            Integer sellerId = goodsInfo.getUid();
            User sellerInfo = userMapper.findUserById(sellerId);
            String sellerName = sellerInfo.getUsername();
            String sellerLocation = sellerInfo.getLocation();
            String sellerTel = sellerInfo.getTel();
            order2.setSellerTel(sellerTel);
            order2.setSellerLocation(sellerLocation);
            order2.setSellerId(sellerId);
            order2.setSellerName(sellerName);
            String gname = goodsInfo.getGname();
            Double price = goodsInfo.getPrice();
            String pic = goodsInfo.getPic();
            //封装orderList
            order2.setOid(orderli.getOid());
            order2.setUserId(orderli.getUserId());
            order2.setUsername(username);
            order2.setGoodId(gid);
            order2.setGname(gname);
            order2.setPrice(price);
            order2.setPic(pic);
            order2.setCreateTime(orderli.getCreateTime());
            order2.setApoTime(orderli.getApoTime());
            order2.setSendTime(orderli.getSendTime());
            order2.setBySelf(orderli.getBySelf());
            order2.setBySend(orderli.getBySend());
            //订单状态
            order2.setStatus(orderli.getStatus());
            orderList.set(i,order2);
        }
        int totalSize = (int) ((Page)orderList).getTotal();
        PageResult pageResult = new PageResult(pageNum,pageSize,totalSize,orderList);
        if(orderList.size()==0){
            return new JsonResult(201,"您暂时没有订单记录");
        }
        return new JsonResult(200,"您的订单",pageResult);
    }

    /*!!买家用户查看已发货待签收订单 order.status为3*/
    public JsonResult findOrderToBeSigned(Integer uid, Integer pageNum, Integer pageSize){
        //设置分页 pageNum当前页 pageSize每页数量
        PageHelper.startPage(pageNum,pageSize);
        List<Order> orderList = orderMapper.findOrderToBeSigned(uid);
        for (int i=0;i<orderList.size();i++){
            Order order2 = new Order();
            Order orderli = orderList.get(i);
            //获取用户信息
            User userInfo = userMapper.findUserById(uid);
            //买家名称
            String username = userInfo.getUsername();
            String tel = userInfo.getTel();
            String buyerLocation = userInfo.getLocation();
            order2.setTel(tel);
            order2.setBuyerLocation(buyerLocation);
            //获取商品信息
            Integer gid = orderli.getGoodId();
            Goods goodsInfo = goodsMapper.findGoodById(gid);
            //获取卖家信息
            Integer sellerId = goodsInfo.getUid();
            User sellerInfo = userMapper.findUserById(sellerId);
            String sellerName = sellerInfo.getUsername();
            String sellerLocation = sellerInfo.getLocation();
            String sellerTel = sellerInfo.getTel();
            order2.setSellerTel(sellerTel);
            order2.setSellerLocation(sellerLocation);
            order2.setSellerId(sellerId);
            order2.setSellerName(sellerName);
            String gname = goodsInfo.getGname();
            Double price = goodsInfo.getPrice();
            String pic = goodsInfo.getPic();
            //封装orderList
            order2.setOid(orderli.getOid());
            order2.setUserId(orderli.getUserId());
            order2.setUsername(username);
            order2.setGoodId(gid);
            order2.setGname(gname);
            order2.setPrice(price);
            order2.setPic(pic);
            order2.setCreateTime(orderli.getCreateTime());
            order2.setApoTime(orderli.getApoTime());
            order2.setSendTime(orderli.getSendTime());
            order2.setBySelf(orderli.getBySelf());
            order2.setBySend(orderli.getBySend());
            //订单状态
            order2.setStatus(orderli.getStatus());
            orderList.set(i,order2);
        }
        int totalSize = (int) ((Page)orderList).getTotal();
        PageResult pageResult = new PageResult(pageNum,pageSize,totalSize,orderList);
        if(orderList.size()==0){
            return new JsonResult(201,"您暂时没有待签收订单记录");
        }
        return new JsonResult(200,"您的待签收订单",pageResult);
    }

    /*!!买家用户查看已签收订单 order.status为4*/
    public JsonResult findOrderSigned(Integer uid, Integer pageNum, Integer pageSize){
        //设置分页 pageNum当前页 pageSize每页数量
        PageHelper.startPage(pageNum,pageSize);
        List<Order> orderList = orderMapper.findOrderSigned(uid);
        for (int i=0;i<orderList.size();i++){
            Order order2 = new Order();
            Order orderli = orderList.get(i);
            //获取用户信息
            User userInfo = userMapper.findUserById(uid);
            //买家名称
            String username = userInfo.getUsername();
            String tel = userInfo.getTel();
            String buyerLocation = userInfo.getLocation();
            order2.setTel(tel);
            order2.setBuyerLocation(buyerLocation);
            //获取商品信息
            Integer gid = orderli.getGoodId();
            Goods goodsInfo = goodsMapper.findGoodById(gid);
            //获取卖家信息
            Integer sellerId = goodsInfo.getUid();
            User sellerInfo = userMapper.findUserById(sellerId);
            String sellerName = sellerInfo.getUsername();
            String sellerLocation = sellerInfo.getLocation();
            String sellerTel = sellerInfo.getTel();
            order2.setSellerTel(sellerTel);
            order2.setSellerLocation(sellerLocation);
            order2.setSellerId(sellerId);
            order2.setSellerName(sellerName);
            String gname = goodsInfo.getGname();
            Double price = goodsInfo.getPrice();
            String pic = goodsInfo.getPic();
            //封装orderList
            order2.setOid(orderli.getOid());
            order2.setUserId(orderli.getUserId());
            order2.setUsername(username);
            order2.setGoodId(gid);
            order2.setGname(gname);
            order2.setPrice(price);
            order2.setPic(pic);
            order2.setCreateTime(orderli.getCreateTime());
            order2.setApoTime(orderli.getApoTime());
            order2.setSendTime(orderli.getSendTime());
            order2.setBySelf(orderli.getBySelf());
            order2.setBySend(orderli.getBySend());
            //订单状态
            order2.setStatus(orderli.getStatus());
            orderList.set(i,order2);
        }
        int totalSize = (int) ((Page)orderList).getTotal();
        PageResult pageResult = new PageResult(pageNum,pageSize,totalSize,orderList);
        if(orderList.size()==0){
            return new JsonResult(201,"您暂时没有已签收订单记录");
        }
        return new JsonResult(200,"您的已签收订单",pageResult);
    }

    /*!!买家用户查退款中订单 order.status为-1*/
    public JsonResult findOrderRefunding(Integer uid, Integer pageNum, Integer pageSize){
        //设置分页 pageNum当前页 pageSize每页数量
        PageHelper.startPage(pageNum,pageSize);
        List<Order> orderList = orderMapper.findOrderRefunding(uid);
        for (int i=0;i<orderList.size();i++){
            Order order2 = new Order();
            Order orderli = orderList.get(i);
            //获取用户信息
            User userInfo = userMapper.findUserById(uid);
            //买家名称
            String username = userInfo.getUsername();
            String tel = userInfo.getTel();
            String buyerLocation = userInfo.getLocation();
            order2.setTel(tel);
            order2.setBuyerLocation(buyerLocation);
            //获取商品信息
            Integer gid = orderli.getGoodId();
            Goods goodsInfo = goodsMapper.findGoodById(gid);
            //获取卖家信息
            Integer sellerId = goodsInfo.getUid();
            User sellerInfo = userMapper.findUserById(sellerId);
            String sellerName = sellerInfo.getUsername();
            String sellerLocation = sellerInfo.getLocation();
            String sellerTel = sellerInfo.getTel();
            order2.setSellerTel(sellerTel);
            order2.setSellerLocation(sellerLocation);
            order2.setSellerId(sellerId);
            order2.setSellerName(sellerName);
            String gname = goodsInfo.getGname();
            Double price = goodsInfo.getPrice();
            String pic = goodsInfo.getPic();
            //封装orderList
            order2.setOid(orderli.getOid());
            order2.setUserId(orderli.getUserId());
            order2.setUsername(username);
            order2.setGoodId(gid);
            order2.setGname(gname);
            order2.setPrice(price);
            order2.setPic(pic);
            order2.setCreateTime(orderli.getCreateTime());
            order2.setApoTime(orderli.getApoTime());
            order2.setSendTime(orderli.getSendTime());
            order2.setBySelf(orderli.getBySelf());
            order2.setBySend(orderli.getBySend());
            //订单状态
            order2.setStatus(orderli.getStatus());
            orderList.set(i,order2);
        }
        int totalSize = (int) ((Page)orderList).getTotal();
        PageResult pageResult = new PageResult(pageNum,pageSize,totalSize,orderList);
        if(orderList.size()==0){
            return new JsonResult(201,"您暂时没有退款中订单记录");
        }
        return new JsonResult(200,"您的退货中订单",pageResult);
    }

/*------------------------------------卖家查订单-------------------------------------------*/

    /*卖家用户在商品管理 查看该商品已支付订单 order.status为2 */
    @Override
    public JsonResult findSellOrder(Integer gid) {
        System.out.println(gid);
        List<Order> sellList = orderMapper.findSellOrder(gid);
        for (int i=0;i<sellList.size();i++){
            Order order2 = new Order();
            Order orderli = sellList.get(i);
            //获取用户信息
            Integer uid = orderli.getUserId();
            User userInfo = userMapper.findUserById(uid);
            //买家名称
            String username = userInfo.getUsername();
            String tel = userInfo.getTel();
            String buyerLocation = userInfo.getLocation();
            order2.setTel(tel);
            order2.setBuyerLocation(buyerLocation);
            //获取商品信息
            Goods goodsInfo = goodsMapper.findGoodById(orderli.getGoodId());
            System.out.println(goodsInfo);
            //获取卖家信息
            Integer sellerId = goodsInfo.getUid();
            User sellerInfo = userMapper.findUserById(sellerId);
            String sellerName = sellerInfo.getUsername();
            String sellerLocation = sellerInfo.getLocation();
            String sellerTel = sellerInfo.getTel();
            order2.setSellerTel(sellerTel);
            order2.setSellerLocation(sellerLocation);
            order2.setSellerId(sellerId);
            order2.setSellerName(sellerName);
            String gname = goodsInfo.getGname();
            Double price = goodsInfo.getPrice();
            String pic = goodsInfo.getPic();
            //封装orderList
            order2.setOid(orderli.getOid());
            order2.setUserId(uid);
            order2.setUsername(username);
            order2.setGoodId(orderli.getGoodId());
            order2.setGname(gname);
            order2.setPrice(price);
            order2.setPic(pic);
            order2.setCreateTime(orderli.getCreateTime());
            order2.setApoTime(orderli.getApoTime());
            order2.setSendTime(orderli.getSendTime());
            order2.setBySelf(orderli.getBySelf());
            order2.setBySend(orderli.getBySend());
            //订单状态
            order2.setStatus(orderli.getStatus());
            sellList.set(i,order2);
        }
        if(sellList.size()==0){
            return new JsonResult(201,"您暂时没有已支付的订单记录");
        }
        return new JsonResult(200,"该商品的已支付订单：",sellList);
    }

    /*卖家用户在订单管理查看所有已支付订单 order.status为2 分页*/
    @Override
    public JsonResult findSellCompletedOrder(Integer uid, Integer pageNum, Integer pageSize) {
        //获取该卖家用户发布的所有商品信息
        List<Goods> goodsList = goodsMapper.findGoodsByUid(uid);
        String[] gids = new String[goodsList.size()];
        //把goodsList对象中的gid属性装进gids数组
        int count=0;
        for(Goods gd : goodsList){
            gids[count] = String.valueOf(gd.getGid());
            count++;
        }
        //判断该用户是否有已发布商品
        if(gids==null||gids.length==0){
            return new JsonResult(201,"暂无卖家商品已支付交易数据");
        }
        //设置分页 pageNum当前页 pageSize每页数量
        PageHelper.startPage(pageNum,pageSize);
        //查询的订单列表
        List<Order> allComOrder = orderMapper.findAllCompletedOrder(gids);
        if(allComOrder==null){
            return new JsonResult(202,"暂无卖家商品成功交易数据");
        }
        for (int i=0;i<allComOrder.size();i++){
            Order order2 = new Order();
            Order orderli = allComOrder.get(i);
            //获取买家用户信息
            Integer buyeruid = orderli.getUserId();
            User userInfo = userMapper.findUserById(buyeruid);
            System.out.println(buyeruid);
            //买家名称
            String username = userInfo.getUsername();
            String tel = userInfo.getTel();
            String buyerLocation = userInfo.getLocation();
            order2.setTel(tel);
            order2.setBuyerLocation(buyerLocation);
            //获取商品信息
            Goods goodsInfo = goodsMapper.findGoodById(orderli.getGoodId());
            System.out.println(goodsInfo);
            //获取卖家信息
            Integer sellerId = goodsInfo.getUid();
            User sellerInfo = userMapper.findUserById(sellerId);
            String sellerName = sellerInfo.getUsername();
            String sellerLocation = sellerInfo.getLocation();
            String sellerTel = sellerInfo.getTel();
            order2.setSellerTel(sellerTel);
            order2.setSellerLocation(sellerLocation);
            order2.setSellerId(sellerId);
            order2.setSellerName(sellerName);
            String gname = goodsInfo.getGname();
            Double price = goodsInfo.getPrice();
            String pic = goodsInfo.getPic();
            //封装orderList
            order2.setOid(orderli.getOid());
            order2.setUserId(buyeruid);
            order2.setUsername(username);
            order2.setGoodId(orderli.getGoodId());
            order2.setGname(gname);
            order2.setPrice(price);
            order2.setPic(pic);
            order2.setCreateTime(orderli.getCreateTime());
            order2.setApoTime(orderli.getApoTime());
            order2.setSendTime(orderli.getSendTime());
            order2.setBySelf(orderli.getBySelf());
            order2.setBySend(orderli.getBySend());
            //订单状态
            order2.setStatus(orderli.getStatus());
            allComOrder.set(i,order2);
        }
        int totalSize = (int) ((Page)allComOrder).getTotal();
        PageResult pageResult = new PageResult(pageNum,pageSize,totalSize,allComOrder);
        if(totalSize==0){
            return new JsonResult(201,"您暂时没有已支付的订单记录");
        }
        return new JsonResult(200,"所有已支付的成交订单：",pageResult);
    }

    /*卖家用户查看订单管理 待签收订单status=3*/
    @Override
    public JsonResult findSellOrderToBeSigned(Integer uid, Integer pageNum, Integer pageSize) {
        //获取该卖家用户发布的所有商品信息
        List<Goods> goodsList = goodsMapper.findGoodsByUid(uid);
        String[] gids = new String[goodsList.size()];
        //把goodsList对象中的gid属性装进gids数组
        int count=0;
        for(Goods gd : goodsList){
            gids[count] = String.valueOf(gd.getGid());
            count++;
        }
        //判断该用户是否有已发布商品
        if(gids==null||gids.length==0){
            return new JsonResult(201,"暂无卖家商品待签收交易数据");
        }
        //设置分页 pageNum当前页 pageSize每页数量
        PageHelper.startPage(pageNum,pageSize);
        //查询的订单列表
        List<Order> allComOrder = orderMapper.findAllOrderToBeSigned(gids);
        if(allComOrder==null){
            return new JsonResult(202,"暂无卖家商品成功待签收数据");
        }
        for (int i=0;i<allComOrder.size();i++){
            Order order2 = new Order();
            Order orderli = allComOrder.get(i);
            //获取买家用户信息
            Integer buyeruid = orderli.getUserId();
            User userInfo = userMapper.findUserById(buyeruid);
            System.out.println(buyeruid);
            //买家名称
            String username = userInfo.getUsername();
            String tel = userInfo.getTel();
            String buyerLocation = userInfo.getLocation();
            order2.setTel(tel);
            order2.setBuyerLocation(buyerLocation);
            //获取商品信息
            Goods goodsInfo = goodsMapper.findGoodById(orderli.getGoodId());
            System.out.println(goodsInfo);
            //获取卖家信息
            Integer sellerId = goodsInfo.getUid();
            User sellerInfo = userMapper.findUserById(sellerId);
            String sellerName = sellerInfo.getUsername();
            String sellerLocation = sellerInfo.getLocation();
            String sellerTel = sellerInfo.getTel();
            order2.setSellerTel(sellerTel);
            order2.setSellerLocation(sellerLocation);
            order2.setSellerId(sellerId);
            order2.setSellerName(sellerName);
            String gname = goodsInfo.getGname();
            Double price = goodsInfo.getPrice();
            String pic = goodsInfo.getPic();
            //封装orderList
            order2.setOid(orderli.getOid());
            order2.setUserId(buyeruid);
            order2.setUsername(username);
            order2.setGoodId(orderli.getGoodId());
            order2.setGname(gname);
            order2.setPrice(price);
            order2.setPic(pic);
            order2.setCreateTime(orderli.getCreateTime());
            order2.setApoTime(orderli.getApoTime());
            order2.setSendTime(orderli.getSendTime());
            order2.setBySelf(orderli.getBySelf());
            order2.setBySend(orderli.getBySend());
            //订单状态
            order2.setStatus(orderli.getStatus());
            allComOrder.set(i,order2);
        }
        int totalSize = (int) ((Page)allComOrder).getTotal();
        PageResult pageResult = new PageResult(pageNum,pageSize,totalSize,allComOrder);
        if(totalSize==0){
            return new JsonResult(201,"您暂时没有待签收的订单记录");
        }
        return new JsonResult(200,"所有待签收的成交订单：",pageResult);
    }

    /*卖家用户查看订单管理 已签收订单status=4*/
    @Override
    public JsonResult findSellOrderSigned(Integer uid, Integer pageNum, Integer pageSize) {
        //获取该卖家用户发布的所有商品信息
        List<Goods> goodsList = goodsMapper.findGoodsByUid(uid);
        String[] gids = new String[goodsList.size()];
        //把goodsList对象中的gid属性装进gids数组
        int count=0;
        for(Goods gd : goodsList){
            gids[count] = String.valueOf(gd.getGid());
            count++;
        }
        //判断该用户是否有已发布商品
        if(gids==null||gids.length==0){
            return new JsonResult(201,"暂无卖家商品已签收交易数据");
        }
        //设置分页 pageNum当前页 pageSize每页数量
        PageHelper.startPage(pageNum,pageSize);
        //查询的订单列表
        List<Order> allComOrder = orderMapper.findAllOrderSigned(gids);
        if(allComOrder==null){
            return new JsonResult(202,"暂无卖家商品已签收交易数据");
        }
        for (int i=0;i<allComOrder.size();i++){
            Order order2 = new Order();
            Order orderli = allComOrder.get(i);
            //获取买家用户信息
            Integer buyeruid = orderli.getUserId();
            User userInfo = userMapper.findUserById(buyeruid);
            System.out.println(buyeruid);
            //买家名称
            String username = userInfo.getUsername();
            String tel = userInfo.getTel();
            String buyerLocation = userInfo.getLocation();
            order2.setTel(tel);
            order2.setBuyerLocation(buyerLocation);
            //获取商品信息
            Goods goodsInfo = goodsMapper.findGoodById(orderli.getGoodId());
            System.out.println(goodsInfo);
            //获取卖家信息
            Integer sellerId = goodsInfo.getUid();
            User sellerInfo = userMapper.findUserById(sellerId);
            String sellerName = sellerInfo.getUsername();
            String sellerLocation = sellerInfo.getLocation();
            String sellerTel = sellerInfo.getTel();
            order2.setSellerTel(sellerTel);
            order2.setSellerLocation(sellerLocation);
            order2.setSellerId(sellerId);
            order2.setSellerName(sellerName);
            String gname = goodsInfo.getGname();
            Double price = goodsInfo.getPrice();
            String pic = goodsInfo.getPic();
            //封装orderList
            order2.setOid(orderli.getOid());
            order2.setUserId(buyeruid);
            order2.setUsername(username);
            order2.setGoodId(orderli.getGoodId());
            order2.setGname(gname);
            order2.setPrice(price);
            order2.setPic(pic);
            order2.setCreateTime(orderli.getCreateTime());
            order2.setApoTime(orderli.getApoTime());
            order2.setSendTime(orderli.getSendTime());
            order2.setBySelf(orderli.getBySelf());
            order2.setBySend(orderli.getBySend());
            //订单状态
            order2.setStatus(orderli.getStatus());
            allComOrder.set(i,order2);
        }
        int totalSize = (int) ((Page)allComOrder).getTotal();
        PageResult pageResult = new PageResult(pageNum,pageSize,totalSize,allComOrder);
        if(totalSize==0){
            return new JsonResult(201,"您暂时没有已签收的订单记录");
        }
        return new JsonResult(200,"所有已签收的成交订单：",pageResult);
    }

    /*卖家用户查看订单管理 待退款订单status=-1*/
    @Override
    public JsonResult findSellRefundedOrder(Integer uid, Integer pageNum, Integer pageSize) {
        //获取该卖家用户发布的所有商品信息
        List<Goods> goodsList = goodsMapper.findGoodsByUid(uid);
        String[] gids = new String[goodsList.size()];
        //把goodsList对象中的gid属性装进gids数组
        int count=0;
        for(Goods gd : goodsList){
            gids[count] = String.valueOf(gd.getGid());
            count++;
        }
        //判断该用户是否有已发布商品
        if(gids==null||gids.length==0){
            return new JsonResult(201,"暂无卖家商品待退款交易数据");
        }
        //设置分页 pageNum当前页 pageSize每页数量
        PageHelper.startPage(pageNum,pageSize);
        //查询的订单列表
        List<Order> allComOrder = orderMapper.findAllRefundedOrder(gids);
        if(allComOrder==null){
            return new JsonResult(202,"暂无卖家商品待退款交易数据");
        }
        for (int i=0;i<allComOrder.size();i++){
            Order order2 = new Order();
            Order orderli = allComOrder.get(i);
            //获取买家用户信息
            Integer buyeruid = orderli.getUserId();
            User userInfo = userMapper.findUserById(buyeruid);
            System.out.println(buyeruid);
            //买家名称
            String username = userInfo.getUsername();
            String tel = userInfo.getTel();
            String buyerLocation = userInfo.getLocation();
            order2.setTel(tel);
            order2.setBuyerLocation(buyerLocation);
            //获取商品信息
            Goods goodsInfo = goodsMapper.findGoodById(orderli.getGoodId());
            System.out.println(goodsInfo);
            //获取卖家信息
            Integer sellerId = goodsInfo.getUid();
            User sellerInfo = userMapper.findUserById(sellerId);
            String sellerName = sellerInfo.getUsername();
            String sellerLocation = sellerInfo.getLocation();
            String sellerTel = sellerInfo.getTel();
            order2.setSellerTel(sellerTel);
            order2.setSellerLocation(sellerLocation);
            order2.setSellerId(sellerId);
            order2.setSellerName(sellerName);
            String gname = goodsInfo.getGname();
            Double price = goodsInfo.getPrice();
            String pic = goodsInfo.getPic();
            //封装orderList
            order2.setOid(orderli.getOid());
            order2.setUserId(buyeruid);
            order2.setUsername(username);
            order2.setGoodId(orderli.getGoodId());
            order2.setGname(gname);
            order2.setPrice(price);
            order2.setPic(pic);
            order2.setCreateTime(orderli.getCreateTime());
            order2.setApoTime(orderli.getApoTime());
            order2.setSendTime(orderli.getSendTime());
            order2.setBySelf(orderli.getBySelf());
            order2.setBySend(orderli.getBySend());
            //订单状态
            order2.setStatus(orderli.getStatus());
            allComOrder.set(i,order2);
        }
        int totalSize = (int) ((Page)allComOrder).getTotal();
        PageResult pageResult = new PageResult(pageNum,pageSize,totalSize,allComOrder);
        if(totalSize==0){
            return new JsonResult(201,"您暂时没有待退款的订单记录");
        }
        return new JsonResult(200,"所有待退款的成交订单：",pageResult);
    }

    /*卖家用户查看订单管理 已失效订单status=0*/
    @Override
    public JsonResult findSellInvalidOrder(Integer uid, Integer pageNum, Integer pageSize) {
        //获取该卖家用户发布的所有商品信息
        List<Goods> goodsList = goodsMapper.findGoodsByUid(uid);
        String[] gids = new String[goodsList.size()];
        //把goodsList对象中的gid属性装进gids数组
        int count=0;
        for(Goods gd : goodsList){
            gids[count] = String.valueOf(gd.getGid());
            count++;
        }
        //判断该用户是否有已发布商品
        if(gids==null||gids.length==0){
            return new JsonResult(201,"暂无卖家商品已失效交易数据");
        }
        //设置分页 pageNum当前页 pageSize每页数量
        PageHelper.startPage(pageNum,pageSize);
        //查询的订单列表
        List<Order> allComOrder = orderMapper.findAllInvalidOrder(gids);
        if(allComOrder==null){
            return new JsonResult(202,"暂无卖家商品已失效交易数据");
        }
        for (int i=0;i<allComOrder.size();i++){
            Order order2 = new Order();
            Order orderli = allComOrder.get(i);
            //获取买家用户信息
            Integer buyeruid = orderli.getUserId();
            User userInfo = userMapper.findUserById(buyeruid);
            System.out.println(buyeruid);
            //买家名称
            String username = userInfo.getUsername();
            String tel = userInfo.getTel();
            String buyerLocation = userInfo.getLocation();
            order2.setTel(tel);
            order2.setBuyerLocation(buyerLocation);
            //获取商品信息
            Goods goodsInfo = goodsMapper.findGoodById(orderli.getGoodId());
            System.out.println(goodsInfo);
            //获取卖家信息
            Integer sellerId = goodsInfo.getUid();
            User sellerInfo = userMapper.findUserById(sellerId);
            String sellerName = sellerInfo.getUsername();
            String sellerLocation = sellerInfo.getLocation();
            String sellerTel = sellerInfo.getTel();
            order2.setSellerTel(sellerTel);
            order2.setSellerLocation(sellerLocation);
            order2.setSellerId(sellerId);
            order2.setSellerName(sellerName);
            String gname = goodsInfo.getGname();
            Double price = goodsInfo.getPrice();
            String pic = goodsInfo.getPic();
            //封装orderList
            order2.setOid(orderli.getOid());
            order2.setUserId(buyeruid);
            order2.setUsername(username);
            order2.setGoodId(orderli.getGoodId());
            order2.setGname(gname);
            order2.setPrice(price);
            order2.setPic(pic);
            order2.setCreateTime(orderli.getCreateTime());
            order2.setApoTime(orderli.getApoTime());
            order2.setSendTime(orderli.getSendTime());
            order2.setBySelf(orderli.getBySelf());
            order2.setBySend(orderli.getBySend());
            //订单状态
            order2.setStatus(orderli.getStatus());
            allComOrder.set(i,order2);
        }
        int totalSize = (int) ((Page)allComOrder).getTotal();
        PageResult pageResult = new PageResult(pageNum,pageSize,totalSize,allComOrder);
        if(totalSize==0){
            return new JsonResult(201,"您暂时没有已失效的订单记录");
        }
        return new JsonResult(200,"所有已失效的成交订单：",pageResult);
    }


}
